<template>
    <div class="cardDetail">
        <detail
            :levelType="$route.params.type"
            :is-export="true"
            :id="$route.query.id"
            :standard-id="$route.query.standardId"
        >
            <template slot="button">
                <el-button
                    v-permission="{
                        type: $route.params.type,
                        per: [185, 186, 187, 188, 189]
                    }"
                    type="primary"
                    @click="handle('evaluation')"
                    >报告评价</el-button
                >
                <el-button
                    v-permission="{
                        type: $route.params.type,
                        per: [190, 191, 192, 193, 194]
                    }"
                    type="primary"
                    @click="exportReport"
                    >导出</el-button
                >
            </template>
        </detail>
    </div>
</template>

<script>
import detail from '../myReport/module/detail.vue';
import { exportWord } from '@/api/report';
export default {
    name: 'cardDetail',
    components: {
        detail
    },
    methods: {
        handle (type) {
            switch (type) {
                case 'evaluation':
                    this.$router.push({
                        path: '/report/reportManage/evaluationList',
                        query: {
                            id: this.$route.query.id
                        }
                    });
                    break;
            }
        },
        exportReport () {
            const canvasList = document.getElementsByTagName('canvas');
            const imgList = [];
            console.log(canvasList, imgList, 'canvasList');

            const str = this.$route.path.split('/');
            imgList.push({
                urlFile: canvasList[0].toDataURL('png'),
                id: 3,
                reportType: str[str.length - 1]
            });
            console.log(canvasList, imgList, 'canvasList');
            exportWord(this.$route.query.id, imgList).then(res => {
                this.downloadFile(res);
            });
        },
        // 下载文件通用方法
        downloadFile (res) {
            //   文件下载
            const url = window.URL.createObjectURL(new Blob([res]));
            const elementA = document.createElement('a');
            elementA.style.display = 'none';
            const fileName = this.$route.query.title + '.docx';
            elementA.href = url;
            elementA.setAttribute('download', fileName);
            document.body.appendChild(elementA);
            elementA.click();
            document.body.removeChild(elementA);
            window.URL.revokeObjectURL(url);
        }
    }
};
</script>

<style scoped></style>
